package com.tingyuge.thinking.spring.resource;

import com.tingyuge.thinking.spring.resource.util.ResourceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import javax.annotation.PostConstruct;
import java.util.stream.Stream;

/**
 * 注入 {@link ResourceLoader} 对象示例
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/22
 * @see Value
 * @see AnnotationConfigApplicationContext
 */
public class InjectResourceLoaderDemo implements ResourceLoaderAware {


    @Autowired
    private ResourceLoader autowiredResourceLoader;//方法1


    private ResourceLoader resourceLoader;//方法2

    @Autowired
    private AbstractApplicationContext abstractApplicationContext; //方法3

    @PostConstruct
    public void init(){
        System.out.println(autowiredResourceLoader==resourceLoader);
        System.out.println(abstractApplicationContext==resourceLoader);
    }
    public static void main(String[] args) {
        //创建beanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(InjectResourceLoaderDemo.class);
        //启动应用上下文
        applicationContext.refresh();
    }


    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
       this.resourceLoader = resourceLoader;
    }
}
